package com.example.appstudy2405;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class ReadTxtActivity extends AppCompatActivity implements View.OnClickListener {

    private static final int REQUEST_CODE_STORAGE_PERMISSION = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_txt);

//        if (ContextCompat.checkSelfPermission(this, Manifest.permission.Rea))
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_STORAGE_PERMISSION);
        }
        findViewById(R.id.bt_read_txt).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.bt_read_txt) {
            // 获取txt文本内容
            TextView tv = findViewById(R.id.tv_show_txt);
            String filePath = Environment.getExternalStorageDirectory() + "/Documents/001&002－Excuse Me.lrc";
            String fileContent = readFileContent(filePath);
            tv.setText(fileContent != null ? fileContent : "读取文件失败");
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE_STORAGE_PERMISSION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 权限被同意，可以访问存储了
                // 这里可以再次尝试读取文件操作
                Log.d("Demo", "权限被同意，可以访问存储了 -- ");
            } else {
                // 权限被拒绝，提示用户或处理拒绝的情况
                Toast.makeText(this, "需要存储权限才能读取文件", Toast.LENGTH_SHORT).show();
            }
        }
    }


    private String readFileContent(String filePath) {
        StringBuilder content = new StringBuilder();
        try {
            File file = new File(filePath);
            if (file.exists() && file.isFile()) {
                BufferedReader reader = new BufferedReader(new FileReader(file));
                String line;
                while ((line = reader.readLine()) != null) {
                    content.append(line).append("\n");
                }
                reader.close();
            }
        } catch (IOException e) {
            Log.e("ReadTxtActivity", "读取文件时发生错误：" + e.getMessage());
        }
        return content.toString();
    }
}